package com.feihong.yw189.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.yw189.R;
import com.feihong.yw189.model.User.MessageItem;
import com.holenzhou.pullrecyclerview.BaseRecyclerAdapter;
import com.holenzhou.pullrecyclerview.BaseViewHolder;

import java.util.ArrayList;
import java.util.Map;

/**
 * 我的页面通用Adapter
 * map和item存放数据一致就可以了
 * Created by PVer on 2017/12/20.
 */
public class MyBaseAdapter extends BaseRecyclerAdapter<MessageItem> {
    private static final String TAG = "营维189";
    private Map<String, Object> map;
    private int type = 0;
    Context mContext;

    public MyBaseAdapter(Context context, int layoutResId, Map<String, Object> map) {
        super(context, layoutResId, new ArrayList<MessageItem>());
        this.map = map;
        this.mContext = context;
    }

    public MyBaseAdapter(Context context, int layoutResId, Map<String, Object> map, int type) {
        super(context, layoutResId, new ArrayList<MessageItem>());
        this.map = map;
        this.mContext = context;
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, MessageItem item) {
        Object title = map.get("title");
        Object subTitle = map.get("subTitle");
        Object time = map.get("time");
        Object message = map.get("message");
        Object order_sn = map.get("order_sn");
        Object imageResId = map.get("imageResId");
        Object imgUrl = map.get("imgUrl");

        helper.itemView.setOnClickListener(v -> {
            if(mItemClickListener != null){
                mItemClickListener.onItemClick(item);
            }
        });
        if(!(""+title).isEmpty() && !(""+title).equals("null")){
            helper.setText((int) title, item.getTitle());
        }
        if(!(""+subTitle).isEmpty() && !(""+subTitle).equals("null")){
            helper.setText((int) subTitle, item.getSubTitle());

            try{
                //系统消息
                if (type == 5 && "系统消息".equals(item.getTitle()) && Integer.parseInt(item.getSubTitle()) > 0) {
                    helper.getView((int) subTitle).setVisibility(View.VISIBLE);
                }
            }catch (Exception e){}
        }
        if(!(""+time).isEmpty() && !(""+time).equals("null")){
            helper.setText((int) time, item.getTime());
        }
        if(!(""+message).isEmpty() && !(""+message).equals("null")){
            helper.setText((int) message, item.getMessage());
            if(type ==1){
                TextView textView = helper.getView((int) message);
                if((""+item.getMessage()).contains("入")){
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.common_font_color_blue));
                }

                else if ((""+item.getMessage()).contains("出")){
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.common_font_color_red));
                }
                else{
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.sub_text));
                }
            }
            //加款记录
            if (type == 3) {
                TextView textView = helper.getView((int) message);
                if((""+item.getMessage()).contains("成功")){
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.common_font_color_red));
                }else{
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.sub_text));
                }
            }

            //转出列表
            if (type == 4) {
                TextView textView = helper.getView((int) message);
                if((""+item.getMessage()).contains("成功")){
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.common_font_color_red));
                }else{
                    textView.setTextColor(mContext.getResources().getColorStateList(R.color.sub_text));
                }
            }
        }
        if(!(""+order_sn).isEmpty() && !(""+order_sn).equals("null")){
            helper.setText((int) order_sn, item.getOrder_sn());
        }

        if(!(""+imageResId).isEmpty() && !(""+imageResId).equals("null")){
            helper.setImageResource((int) imageResId, item.imageResId);
        }

        if(!(""+imgUrl).isEmpty() && !(""+imgUrl).equals("null")){
            ImageView avatarView = helper.getView((int) imgUrl);
            Glide.with(mContext)
                    .load(item.getImgUrl())
                    .into(avatarView);
        }
    }
    private MessageItemClickListener mItemClickListener;
    public void setOnItemClickListener(MessageItemClickListener itemClickListener){
        mItemClickListener = itemClickListener;
    }
    public interface MessageItemClickListener{
        void onItemClick(MessageItem messageItem);
    }
}
